############################################################
# auther	: sgxiao
# Date		: 2008-12-26
# version	: 0.3
# history
#	sgxiao	0.3	add a $ARGV[0] paraterments
#	sgxiao	0.2	modify the column of right price , reform the return formula by changing to the right price
#	sgxiao	0.1	implements a format of month.return
#############################################################

#!/usr/bin/perl
use Spreadsheet::WriteExcel;      
	my $workbook = Spreadsheet::WriteExcel->new("$ARGV[0].xls"); # Step 1
	open CONFIG, "$ARGV[0].whole.SMW";



#arguments list
# 1.worksheet	2.average_days 1 2 5 10 50 100 150 200	3. $jj	4. left_num	5.right_num	6.sheet1	7.state1
sub caculate_return {

# = @array;
my ($worksheet, $num, $kk, $left_num, $right_num, $last, ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j), $close_price_buy, $right_price_buy) = @_;
	my $current;
	my $close_price_return="";
	my $right_price_return="";


	#printf("incur: ($worksheet, $num, $kk, $left_num, $right_num, $last ,$close_price_buy, $right_price_buy)\n");
	$worksheet->write($kk , 0, $a);
	$worksheet->write($kk , 1, $j);
	$worksheet->write($kk , 2, $left_num);
	$worksheet->write($kk , 3, $right_num);
	
	my $jj2 = $kk + 1;
	if($kk >= ($num - 1)) {
		##
		#printf("%f %f last %d  \n", $left_num, $right_num, $last);
		


		if( $last == 0 ) {
			if($left_num > $right_num) {
				$current = 1;
				#$close_price_buy=$b;
				$right_price_buy=$j;	
			} else {
				$current = 0;
			}	
		} else {
			if($left_num > $right_num) {
				$current = 1;
			} else {
				$current=0;				
			}
				#$close_price_return =(($b - $close_price_buy)/$close_price_buy);
				$right_price_return =(($j - $right_price_buy)/$right_price_buy);
				
				#$worksheet->write($kk, 4, "$right_price_return");
				$worksheet->write_formula($kk, 5, "=B$jj2/B$kk-1)" );
				printf("%f %f last %d \n", $left_num, $right_num, $last);
		}

		$worksheet->write_formula($kk, 4, "=IF(C$jj2>D$jj2, \"True\", \"False\")" );
		$last = $current;
	}

	##
	#system "echo $a\\\t$left_num\\\t$right_num\\\t\"=IF(B\$jj2>C\$jj2, \"True\", \"False\")\"\\\t$right_price_return >> $worksheet";
	($last, $close_price_buy, $right_price_buy);
}






####  main()



$worksheet1 = $workbook->add_worksheet('1_50'); 
$worksheet2 = $workbook->add_worksheet('1_150'); 
$worksheet3 = $workbook->add_worksheet('1_200'); 
$worksheet4 = $workbook->add_worksheet('2_200'); 
$worksheet5 = $workbook->add_worksheet('5_150'); 
$worksheet6 = $workbook->add_worksheet('10_200');


#$worksheet1 = "1_50.month.return";
#$worksheet2 = "1_150.month.return";
#$worksheet3 = "1_200.month.return";
#$worksheet4 = "2_200.month.return";
#$worksheet5 = "5_150.month.return";
#$worksheet6 = "10_200.month.return";

#system "echo \"\" > $worksheet1";
#system "echo \"\" > $worksheet2";
#system "echo \"\" > $worksheet3";
#system "echo \"\" > $worksheet4";
#system "echo \"\" > $worksheet5";
#system "echo \"\" > $worksheet6";

my $last1=0;
my $close_price_buy1_50;
my $right_price_buy1_50;
my $close_price_return1_50;
my $right_price_return1_50;

my $last2=0;
my $close_price_buy1_150;
my $right_price_buy1_150;
my $close_price_return1_150;
my $right_price_return1_150;

my $last3=0;
my $close_price_buy1_200;
my $right_price_buy1_200;
my $close_price_return1_200;
my $right_price_return1_200;

my $last4=0;
my $close_price_buy2_200;
my $right_price_buy2_200;
my $close_price_return2_200;
my $right_price_return2_200;

my $last5=0;
my $close_price_buy5_150;
my $right_price_buy5_150;
my $close_price_return5_150;
my $right_price_return5_150;

my $last6=0;
my $close_price_buy10_200;
my $right_price_buy10_200;
my $close_price_return10_200;
my $right_price_return10_200;


$jj=0;




while(<CONFIG>) {
	chomp;
	@array = split /\t/, $_;

	#	a 	b 	c 	d 	e 	f	 g 	h 	i 	j
	#	d 	1 	2 	5 	10 	50 	100	150	200	r
	
	my($a, $b, $c, $d, $e, $f, $g, $h, $i, $j) = @array;
	# 1.worksheet	2.average_days 1 2 5 10 50 100 150 200		4. left_num	5.right_num	6.sheet1	7.state1
	
#printf("1_50\n"); 
	($last1, $close_price_buy1_50, $right_price_buy1_50) = &caculate_return($worksheet1, 50, $jj, $j, $f, $last1, @array, $close_price_buy1_50, $right_price_buy1_50);
#printf("1_150\n"); 
	($last2, $close_price_buy1_150, $right_price_buy1_150) = &caculate_return($worksheet2, 150, $jj, $j, $h, $last2, @array, $close_price_buy1_150, $right_price_buy1_150);
#printf("1_200\n"); 
	($last3, $close_price_buy1_200, $right_price_buy1_200) = &caculate_return($worksheet3, 200, $jj, $j, $i, $last3, @array, $close_price_buy1_200, $right_price_buy1_200);
#printf("2_200\n"); 
	($last4, $close_price_buy2_200, $right_price_buy2_200) = &caculate_return($worksheet4, 200, $jj, $c, $i, $last4, @array, $close_price_buy2_200, $right_price_buy2_200);
#printf("5_150\n"); 	
	($last5, $close_price_buy5_150, $right_price_buy5_150) = &caculate_return($worksheet5, 150, $jj, $d, $h, $last5, @array, $close_price_buy5_150, $right_price_buy5_150);
#printf("10_200\n"); 	
	($last6, $close_price_buy10_200, $right_price_buy10_200) = &caculate_return($worksheet6, 200, $jj, $e, $i, $last6, @array, $close_price_buy10_200, $right_price_buy10_200);
printf("%d...\n", $jj);
	$jj++;
}